// index.js
var common = require('../../utils/userstatus');
var util = require('../../utils/util');

Page({
  data: {
    info: "",
    userStatus: wx.getStorageSync('userStatusList'),
    eventChannel: "",
    appInstance: ""
  },
  onLoad(options) {
    var appInstance = getApp();
    this.data.appInstance = appInstance;
    this.data.eventChannel = this.getOpenerEventChannel();
    this.data.eventChannel.on('infoData', (data) => {
      this.setData({
        info: data.data
      })
    })
  },
  selectStatus(e) {
    var index = e.currentTarget.dataset.index;
    var value = this.data.userStatus[index].value;
    this.setData({
      info: value
    })
  },
  inputValueChange(event) {
    this.data.info = event.detail.value
  },
  updateUserStatus() {
    var url = '/applet/updateUserInfo';
    var id = wx.getStorageSync('userInfo').id
    var data = {};
    data.id = id;
    if (util.isBlank(this.data.info)) {
      wx.showToast({
        title: '请输入有效字符',
        icon: 'error',
        duration: 2000
      })
      return;
    }
    data.userStatusStr = this.data.info;
    util.postRequest(url,
      data,
      (data) => {
        if (data.code == 2000) {
          var userInfo = wx.getStorageSync('userInfo');
          userInfo.userStatusStr = this.data.info;
          wx.setStorageSync('userInfo', userInfo)
          this.data.eventChannel.emit('backShow', {
            data: userInfo
          })
          wx.navigateBack();
        } else {
          if (data.code == 1001) {
            console.log(data.data.maxUpdateTimes)
            wx.showToast({
              title: '超修改最大次数',
              icon: 'error',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: '修改失败',
              icon: 'error',
              duration: 2000
            })
          }
        }
      },
      (error) => {
        wx.showToast({
          title: '修改失败',
          icon: 'error',
          duration: 2000
        })
      })
  }
})